package com.atguigu.spring5.testdemo;

import com.atguigu.spring5.autowire.Emp;
import com.atguigu.spring5.bean.Orders;
import com.atguigu.spring5.collectiontype.Book;
import com.atguigu.spring5.collectiontype.Course;
import com.atguigu.spring5.collectiontype.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5Demo1 {

    @Test
    public void testCollection() {

        ApplicationContext context
                = new ClassPathXmlApplicationContext("collectionbean1.xml");

        Stu stu = context.getBean("stu", Stu.class);

        stu.test();

    }


    @Test
    public void testCollection2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("collectionbean2.xml");

        Book book = context.getBean("book", Book.class);

        book.test();


    }

    @Test
    public void testMyBean() {
        ApplicationContext pathXmlApplicationContext = new ClassPathXmlApplicationContext("bean3.xml");

        Course course = pathXmlApplicationContext.getBean("myBean", Course.class);

        System.out.println(course.getString());


    }

    /**
     * spring默认情况下bean是单例
     * 测试如下
     */
    @Test
    public void testSingle() {
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean3.xml");

        //如果两个对象
        Course course1 = classPathXmlApplicationContext.getBean("course", Course.class);
        Course course2 = classPathXmlApplicationContext.getBean("course", Course.class);

        System.out.println(course1.equals(course2)); //true

        System.out.println(course1);
        System.out.println(course2);

    }


    @Test
    public void testBean3() {
//        ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");


        Orders orders = context.getBean("orders", Orders.class);

        System.out.println("第四步，获取bean的实例对象");
        System.out.println(orders);

        //手动让bean销毁
//        ((ClassPathXmlApplicationContext)context).close();

        //手动让bean销毁
        context.close();

    }


    @Test
    public void test4() {
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean5.xml");

        Emp emp = classPathXmlApplicationContext.getBean("emp", Emp.class);
        System.out.println(emp);

    }


}
